﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace HDJ.Framework.Core
{
    public class TriggerAreas : MonoBehaviour
    {
        public CallBack<Collider> OnTriggerEnterCallBack;
        public CallBack<Collider> OnTriggerStayCallBack;
        public CallBack<Collider> OnTriggerExitCallBack;

        public CallBack<Collision> OnCollisionEnterCallBack;
        /// <summary>
        /// 某个物体碰撞一次时，的第一次接触
        /// </summary>
        public CallBack<Collision> OnCollisionOneEnterCallBack;
        /// <summary>
        /// 某个物体碰撞一次时，的最后脱离接触
        /// </summary>
        public CallBack<Collision> OnCollisionOneExitCallBack;
        [HideInInspector]
        public Collider m_collider;
        public LayerMask layer;
        // Use this for initialization
        void Awake()
        {

            m_collider = GetComponent<Collider>();
            if (m_collider)
            {
                m_collider.isTrigger = true;
            }
        }

        private void OnTriggerEnter(Collider other)
        {
            if (OnTriggerEnterCallBack != null)
            {
                OnTriggerEnterCallBack(other);
            }
        }
        private void OnTriggerStay(Collider other)
        {
            if (OnTriggerStayCallBack != null)
            {
                OnTriggerStayCallBack(other);
            }
        }
        private void OnTriggerExit(Collider other)
        {
            if (OnTriggerExitCallBack != null)
            {
                OnTriggerExitCallBack(other);
            }
        }
        Dictionary<Transform, List<Transform>> atackOnceTempDic = new Dictionary<Transform, List<Transform>>();
        private void OnCollisionEnter(Collision collision)
        {
            LayerMask objLayer = 1 << collision.gameObject.layer;
            if ((layer & objLayer) != 0)
            {
                //Transform root = collision.transform.root;
                //PuppetMaster puppetMaster= root.GetComponentInChildren<PuppetMaster>();
                //if (puppetMaster)
                //{
                //    foreach (var item in puppetMaster.muscles)
                //    {
                //        if(item.joint.transform == collision.transform && item.props.group== Muscle.Group.Arm)
                //        {
                //            return;
                //        }
                //    }
                ////}
                //if (atackOnceTempDic.ContainsKey(root))
                //{
                //    atackOnceTempDic[root].Add(collision.transform);
                //}
                //else
                //{
                //    //Debug.Log("第一次碰撞：" + collision.transform.name +" layer :"+LayerMask.LayerToName(collision.gameObject.layer) );
                //    atackOnceTempDic.Add(root, new List<Transform>() { collision.transform });

                //    if (OnCollisionOneEnterCallBack != null)
                //    {
                //        OnCollisionOneEnterCallBack(collision);
                //    }
                //}


                if (OnCollisionEnterCallBack != null)
                {
                    OnCollisionEnterCallBack(collision);
                }
            }
        }

        private void OnCollisionExit(Collision collision)
        {
            LayerMask objLayer = 1 << collision.gameObject.layer;
            if ((layer & objLayer) != 0)
            {
                //Transform root = collision.transform.root;
                //PuppetMaster puppetMaster = root.GetComponentInChildren<PuppetMaster>();
                //if (puppetMaster)
                //{
                //    foreach (var item in puppetMaster.muscles)
                //    {
                //        if (item.joint.transform == collision.transform && item.props.group == Muscle.Group.Arm)
                //        {
                //            return;
                //        }
                //    }
                //}

                //atackOnceTempDic[root].Remove(collision.transform);

                //if (atackOnceTempDic[root].Count == 0)
                //{
                //    atackOnceTempDic.Remove(root);
                //    if (OnCollisionOneExitCallBack != null)
                //    {
                //        OnCollisionOneExitCallBack(collision);
                //    }
                //    //Debug.LogError("碰撞一次完成 ：" + collision.transform.name);
                //}
            }
        }
    }
}
